#encoding:utf-8
from __future__ import unicode_literals
from django.contrib.auth.models import AbstractUser
from django.db import models

# Create your models here.
class User(AbstractUser):
    #添加手机的登录方方式
    phone_num = models.CharField(max_length=11, blank=True, null=True, unique=True, verbose_name='手机号码')

    class Meta:
        verbose_name = '用户'
        verbose_name_plural = verbose_name
        ordering = ['-id']

    def __unicode__(self):
        return self.username

class Tags(models.Model):
    name = models.CharField(max_length = 30,verbose_name='标签名称')

    class Meta:
        verbose_name = '标签'
        verbose_name_plural = verbose_name

    def __unicode__(self):
        return self.name

class News(models.Model):
    tittle = models.CharField(max_length=30,blank = True,null =True,verbose_name='标题')
    desc = models.CharField(max_length=200,blank = True,null =True,verbose_name='简介')
    click_count = models.IntegerField(default=0, verbose_name='点击次数')
    date_publish = models.DateField(auto_now_add=True,verbose_name='发布时间')
    content = models.TextField(verbose_name='内容')
    tags = models.ManyToManyField(Tags,verbose_name ='标签')
    img = models.ImageField(upload_to='avator/%Y/%m',default ='avator/default', verbose_name='新闻图片') # 默认的保存路径为最外层mysite_project中 即为根目录 (此外保存在media_root )+upload_to

    class Meta:
        permissions = (('view','can see News'),
                    ('read','can read News'),
                    ('create','can create News'),
                    ('delete','can delete News'),)
        verbose_name = '新闻'
        verbose_name_plural = verbose_name
        ordering = ['-id']

    def __unicode__(self):
        return self.tittle

class Comments(models.Model):
    content = models.TextField(verbose_name='评论内容')
    username = models.CharField(max_length=30, blank=True, null=True, verbose_name='用户名')
    email = models.EmailField(max_length=50, blank=True, null=True, verbose_name='邮箱地址')
    url = models.URLField(max_length=100, blank=True, null=True, verbose_name='个人网页地址')
    date_publish = models.DateTimeField(auto_now_add=True, verbose_name='发布时间')
    user = models.ForeignKey(User, blank=True, null=True, verbose_name='用户')
    news = models.ForeignKey(News, blank=True, null=True, verbose_name='文章')
    pid = models.ForeignKey('self', blank=True, null=True, verbose_name='父级评论')

    class Meta:
        verbose_name = '评论'
        verbose_name_plural = verbose_name

    def __unicode__(self):
        return str(self.id)


